package com.lenovo.manufacture.module;

import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.adapter.MyAdapter;
import com.lenovo.manufacture.annotation.Layout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class Module5 extends BaseActivity {
    List<Map<String, String>> materials;
    @BindView(R.id.tv_price)
    BootstrapLabel tvPrice;
    @BindView(R.id.tv_num)
    BootstrapLabel tvNum;
    @BindView(R.id.rv_module5)
    RecyclerView rvModule5;

    @Layout(value = R.layout.activity_module5, title = "供货列表")
    protected void init() {
        async(apiService.getMaterial(), data -> {
            materials = data.getData();
            initList();
        });
    }

    private void initList() {
        List<String> strings = new ArrayList<>();
        materials.forEach(material -> {
            strings.add(material.get("materialName"));
            strings.add(decimalFormat.format(Integer.parseInt(material.get("price"))));
            strings.add(material.get("num"));
            strings.add(material.get("supplyName"));
        });
        MyAdapter myAdapter = new MyAdapter(strings);
        rvModule5.setLayoutManager(new GridLayoutManager(this, 4));
        rvModule5.setAdapter(myAdapter);
    }

    @OnClick({R.id.tv_price, R.id.tv_num})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_price:
                sort(materials, "price", tvPrice, "价钱");
                break;
            case R.id.tv_num:
                sort(materials, "num", tvNum, "数量");
                break;
        }
        initList();
    }
}
